package com.grq.mis.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.grq.mis.entity.Permission;
import com.grq.mis.entity.Role;
import com.grq.mis.entity.RolePermission;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.view.PermissionView;
import com.grq.mis.service.PermissionService;
import com.grq.mis.service.RolePermissionService;
import com.grq.mis.service.RoleService;
import com.grq.mis.validate.RoleValidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 列表
     *
     * @param model
     * @return
     */
    @SaCheckPermission("role:list")
    @GetMapping("/list")
    public String list(Model model) {

        List<Role> roles = roleService.all();
        model.addAttribute("roles", roles);

        return "role/list.html";
    }

    /**
     * 添加角色页面
     *
     * @param model
     * @return
     */
    @SaCheckPermission("role:add")
    @GetMapping("/addForm")
    public String addForm(Model model) {

        // 查出权限列表
        List<PermissionView> tops = permissionService.createLambdaQuery().andEq(Permission::getParentId, 0).asc(Permission::getSort).select(PermissionView.class);
        model.addAttribute("tops", tops);

        return "role/add.html";
    }

    @SaCheckPermission("role:add")
    @PostMapping("/add")
    public String add(Role role, Long[] permissionIds, Model model) {

        // 验证
        RoleValidate.add(role);

        roleService.addRole(role, permissionIds);

        return frameSuccess(model, ResultEnum.SUCCESS, "添加角色成功");
    }

    @SaCheckPermission("role:update")
    @GetMapping("/updateForm")
    public String updateForm(@RequestParam Long id, Model model) {

        Role role = roleService.single(id);
        if (role == null) {
            throw new BusinessException(ResultEnum.ROLE_IS_NULL);
        }

        model.addAttribute("role", role);

        // 角色拥有的权限
        List<RolePermission> rolePermissions = rolePermissionService.createLambdaQuery().andEq(RolePermission::getRoleId, role.getId()).select();
        List<Long> userPermissionIds = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            userPermissionIds.add(rolePermission.getPermissionId());
        }
        model.addAttribute("userPermissionIds", userPermissionIds);

        // 查出权限列表
        List<PermissionView> tops = permissionService.createLambdaQuery().andEq(Permission::getParentId, 0).asc(Permission::getSort).select(PermissionView.class);
        model.addAttribute("tops", tops);
        return "role/update.html";
    }

    @SaCheckPermission("role:update")
    @PostMapping("/update")
    public String update(Role role, Long[] permissionIds, Model model) {

        // 验证
        RoleValidate.update(role);

        roleService.updateRole(role, permissionIds);

        return frameSuccess(model, ResultEnum.SUCCESS, "修改角色成功");
    }

    @SaCheckPermission("role:delete")
    @GetMapping("/delete")
    public String delete(@RequestParam Long id, RedirectAttributes attr) {

        roleService.deleteRole(id);

        return pageSuccess(attr, "/role/list", ResultEnum.SUCCESS, "删除角色成功");
    }

}
